package com.mgetool.ui.animation;

import java.util.Vector;

import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;

import com.mgetool.ui.CanvasComposite;
import com.mgetool.ui.DrawableInterface;
import com.mgetool.ui.XRect;
import com.mgetool.ui.drawer.ScallingParam;
import com.mgetool.ui.util.PointF;




/**
 * 动画类	目前实现了移动、缩放、透明渐变功能
 * @author wangbin
 *
 */
public class IAnimation extends AbstractAnimation{
	//帧容器
	private Vector<Frame> frames;
	private PointF curPosition;// 当前坐标;
	private PointF startPosition;// 起始坐标;
	private PointF endPosition;// 结束坐标;
	private PointF prePosintion;	//每次需要移动的距离
	//当前播放帧索引
	private int curFrameIndex;
	private long lastMills;
	private long lastDelay;
	protected int loopNum;
	private int curLoop;
	private int[] durations;
	private int durationIdx;
	//播放总时间
	private int duration;
	//当前需要延迟的时间
	private int preDuration;
	//起始大小
	private PointF startScaling;
	//目标大小
	private PointF endScaling;
	//当前要改变的大小
	private PointF curScaling;
	//缩放锚点
	private int scallingAnchor;
	
	//每次需要改变的大小
	private PointF preScaling;
	//起始透明度
	private int startAlpha = 255;
	//目标透明度
	private int endAlpha = 255;
	//当前要改变的透明度
	private float curAlpha;
	//每次需要改变的透明度
	private float preAlpha;
	//当前动画是否播放
	private boolean isPlay;
	//当前播放帧
	private Frame curFrame;
	//动画是否播放完毕
	private boolean isEnd;
	//不移动
	public static final byte MOVE_DIRECTION_NONE = 0x0;
	public static final byte MOVE_DIRECTION_UP = 0x1;
	public static final byte MOVE_DIRECTION_DOWN = 0x2;
	public static final byte MOVE_DIRECTION_LEFT = 0x4;
	public static final byte MOVE_DIRECTION_RIGHT = 0x8;
	//移动方向
	private byte moveDirection;
	//不缩放
	private static final byte SCALING_NONE = 0x0;
	private static final byte SCALING_X_ZOOM_IN = 0x1;
	private static final byte SCALING_X_ZOOM_OUT = 0x2;
	private static final byte SCALING_Y_ZOOM_IN = 0x4;
	private static final byte SCALING_Y_ZOOM_OUT = 0x8;
	//移动方向
	private byte scalingType;
	//动画类型
	private byte type;
	//动画监听器
	private Vector<IAnimationListener> listeners;
	//是否保留动画完毕后的信息 比如位置、大小、透明度等
	private boolean isUseAfterAnimation;
	//临时记录动画的初始位置、大小、透明度
	private PointF tempPosition;
	private int tempAlpha;
	private PointF tempScaling;
	//动画播放起始时间
	private long startPlayTime;
	//子类型
	private byte subType;
	//是否暂停
	private boolean isPause;
	//背景
//	private Bitmap backGround;
	//移动路径
	private Point[] movePath;
	private int movePathIndex;
	private DrawableInterface backGround;
	//按照移动路径移动时，是否前后节点相连
	private boolean isSeemlessPath;
	//是否是按照指定时间来播放
	private boolean isDuration;
	//移动路径中每次的速度
	private PointF[] pathSpeed;
	//移动路径的速度索引
	private int pathSpeedIndex;
	//移动结束条件判断
	//移动到终点
	public static final byte MOVE_JUDGE_STYLE_MOVE_2_END = 0x1;
	//移动时间终点
	public static final byte MOVE_JUDGE_STYLE_MOVE_TIME_END = 0x2;
	private int moveJudgeStyle;
	//旋转角度
	private float curRotate;
	//是否应用于速度
	private boolean isApplySpeed;
	//是否绘制碰撞框
	private boolean isDrawCollideFrame;
	/**
	 * 动画播放至最后一帧时重复播放
	 */
	public static final byte STYLE_LOOP_LAST_FRAME = 0x1;
	
	private int style;
	//翻转动画
	private int flip;
	
	public IAnimation(){
		init();
	}
	
	@Override
	public void appendFrame(Frame frame) {
		// TODO Auto-generated method stub
		frames.addElement(frame);
		if(frame != null){
			setSize(frame.getWidth(), frame.getHeight());
		}
	}

	@Override
	public Frame getCurFrame() {
		// TODO Auto-generated method stub
		if(curFrameIndex > -1 && curFrameIndex < frames.size()){
			return frames.elementAt(curFrameIndex);
		}
		return null;
	}

	/**
	 * 得到指定索引上的帧
	 * @param index
	 * @return
	 */
	public Frame getFrame(int index){
		if(index > -1 && index < frames.size()){
			return frames.elementAt(index);
		}
		return null;
	}
	
	@Override
	public boolean isEnd() {
		// TODO Auto-generated method stub
		return isEnd;
	}

	@Override
	public boolean isPlay() {
		// TODO Auto-generated method stub
		return isPlay;
	}

	@Override
	public void nextFrame() {
		// TODO Auto-generated method stub
		updataFramIdx();
		curFrame = getCurFrame();
	}

	@Override
	public void play() {
		// TODO Auto-generated method stub
		curFrame = getCurFrame();
		updataMovePath();
		updataDuration();
		initAnimationParam();
		setEnd(false);
		setPlay(true);
	}

	/**
	 * 播放下一个移动路径点
	 */
	private void playNextPath(){
		curFrame = getCurFrame();
		updataDuration();
		initAnimationParam();
	}
	
	/**
	 * 初始化动画的相关参数
	 */
	private void initAnimationParam(){
		preScaling = getPreScaling();
		prePosintion = getMovePoint();
		preAlpha = getPreAlpha();
		preRotate = getPreRotate();
		startPlayTime = System.currentTimeMillis();
	}
	
	@Override
	public void reset() {
		// TODO Auto-generated method stub
		setPlay(false);
		curFrameIndex = 0;
		curLoop = 0;
		noticeAnimationPlayIdx(0, curFrameIndex);
		movePathIndex = 0;
		pathSpeedIndex = 0;
		curRotate = 0;
		curAlpha = 255;
	}

	/**
	 * 更新当前帧数据
	 */
	public void updataDuration(){
		if(curFrame != null){
			lastDelay = curFrame.getDelay();
			lastMills = System.currentTimeMillis();
		}
	}
	
	public void setEnd(boolean isEnd){
		this.isEnd = isEnd;
		if(isEnd){
			if((getStyle() & STYLE_LOOP_LAST_FRAME) == 0){
				setPlay(false);
			}
			noticeAnimationEndListener();
		}
	}
	
	/**
	 * 使用动画播放完毕后的数据
	 */
	public void useAfterAnimationData(){
		tempPosition = new PointF(startPosition.x, startPosition.y);
		setMove(new PointF(endPosition.x, endPosition.y), new PointF(endPosition.x, endPosition.y));
		tempAlpha = startAlpha;
		curAlpha = endAlpha;
		if(endScaling != null && startScaling != null){
			tempScaling = new PointF(startScaling.x, startScaling.y);
			curScaling = new PointF(endScaling.x, endScaling.y);
		}
	}
	
	public void useBeforeAnimationData(){
		tempPosition = new PointF(endPosition.x, endPosition.y);
		setMove(new PointF(startPosition.x, startPosition.y), new PointF(startPosition.x, startPosition.y));
		tempAlpha = endAlpha;
		curAlpha = startAlpha;
		if(endScaling != null && startScaling != null){
			tempScaling = new PointF(endScaling.x, endScaling.y);
			curScaling = new PointF(startScaling.x, startScaling.y);
		}
	}
	
	/**
	 * 通知动画播放到第几帧了
	 */
	protected void noticeAnimationPlayIdx(int animationIdx, int frameIdx){
		if(listeners != null){
			for(int i = 0; i < listeners.size(); i++){
				listeners.elementAt(i).animationPlayIndex(this, animationIdx, frameIdx);
			}
		}
	}
	
	/**
	 * 通知动画结束
	 */
	protected void noticeAnimationEndListener(){
		if(listeners != null){
			for(int i = 0; i < listeners.size(); i++){
				listeners.elementAt(i).animationEnd(this);
			}
		}
	}
	
	@Override
	public void setPlay(boolean isPlay) {
		// TODO Auto-generated method stub
		this.isPlay = isPlay;
		isPause = false;
	}

	@Override
	protected void updataAlpha() {
		// TODO Auto-generated method stub
		if(preAlpha != -1){
			float a = preAlpha;
			if(preAlpha > 0){
				if(curAlpha + preAlpha > endAlpha){
					a = endAlpha - curAlpha;
				}
			}else{
				if(curAlpha + preAlpha < endAlpha){
					a = endAlpha - curAlpha;
				}
			}
			curAlpha += a;
		}
	}

	@Override
	public void updataFramIdx() {
		// TODO Auto-generated method stub
		if(!isEnd() && isCanNextFrame()){
			curFrameIndex++;
			updataLoop();
			updataDuration();
			if(!isEnd()){
				noticeAnimationPlayIdx(0, curFrameIndex);
			}
		}
	}

	@Override
	public void updataLoop() {
		// TODO Auto-generated method stub
		if (curFrameIndex > frames.size() - 1) {
			curFrameIndex = 0;
			curLoop++;
			if(! isEnd() && loopNum > 0 && curLoop > loopNum - 1){
				setEnd(true);
				if(isUseAfterAnimation()){
					curFrameIndex = frames.size() - 1;
				}
			}
		}
	}

	@Override
	protected void updataPosition() {
		// TODO Auto-generated method stub
//		moveDirection = getMoveDirection(this.startPosition, this.endPosition);
		if(moveDirection != MOVE_DIRECTION_NONE){
			PointF f = new PointF(prePosintion.x, prePosintion.y);
			if(f != null){
				if((moveDirection & MOVE_DIRECTION_LEFT) != 0){
					if(curPosition.x + f.x < endPosition.x){
						f.x = endPosition.x - curPosition.x;
					}
				}else if((moveDirection & MOVE_DIRECTION_RIGHT) != 0){
					if(curPosition.x + f.x > endPosition.x){
						f.x = endPosition.x - curPosition.x;
					}
				}
				
				if((moveDirection & MOVE_DIRECTION_UP) != 0){
					if(curPosition.y + f.y < endPosition.y){
						f.y = endPosition.y - curPosition.y;
					}
				}else if((moveDirection & MOVE_DIRECTION_DOWN) != 0){
					if(curPosition.y + f.y > endPosition.y){
						f.y = endPosition.y - curPosition.y;
					}
				}
				curPosition.offset(f.x, f.y);
				setPositionByMove((int)curPosition.x, (int)curPosition.y);
			}
		}
	}

	@Override
	protected void updataScaling() {
		// TODO Auto-generated method stub
		if(scalingType != SCALING_NONE){
			PointF f = preScaling;
			if(f != null){
				if((scalingType & SCALING_X_ZOOM_IN) != 0){
					if(curScaling.x + f.x < endScaling.x){
						f.x = endScaling.x - curScaling.x;
					}
				}else if((scalingType & SCALING_X_ZOOM_OUT) != 0){
					if(curScaling.x + f.x > endScaling.x){
						f.x = endScaling.x - curScaling.x;
					}
				}
				
				if((scalingType & SCALING_Y_ZOOM_IN) != 0){
					if(curScaling.y + f.y < endScaling.y){
						f.y = endScaling.y - curScaling.y;
					}
				}else if((scalingType & SCALING_Y_ZOOM_OUT) != 0){
					if(curScaling.y + f.y > endScaling.y){
						f.y = endScaling.y - curScaling.y;
					}
				}
				curScaling.offset(f.x, f.y);
			}
		}
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		frames = new Vector<Frame>();
		loopNum = 1;
		curAlpha = -1;
		setUseAfterAnimation(true);
	}

	@Override
	public void onDraw(GC gc) {
		// TODO Auto-generated method stub
		if(isPlay()){
			onDrawBackGround(gc);
			if(curFrame != null){
//				if(curScaling != null || curAlpha != -1){
				ScallingParam scallingParam = null;
				if(curScaling != null){
					scallingParam = new ScallingParam(curScaling, scallingAnchor);
				}
				int rotate = (int)curRotate % 360;
					curFrame.draw(gc, (int)curPosition.x, (int)curPosition.y, rotate | getFlip(), scallingParam, (int)curAlpha);
//				}else{
//					curFrame.draw(canvas, (int)curPoint.x, (int)curPoint.y, paint);
//				}
				if(isDrawCollideFrame()){
					testDrawFrame(gc);
				}
			}
			if(!isPause){
				nextFrame();
				updataPosition();
				updataScaling();
				updataAlpha();
				updateRotate();
				updataPlayTime();
			}
			//test
//			testDrawFrame(canvas, paint);
		}
	}

	private void testDrawFrame(GC gc){
		if(getCurFrame() != null && getCurFrame().getCollideRegions() != null){
			Vector<XRect> rects = getCurFrame().getCollideRegions();
			for(int i = 0; i < rects.size(); i++){
				XRect rect = rects.elementAt(i);
				int curX = getX() + rect.getX();
				int curY = getY() + rect.getY();
				int curW = rect.getWidth();
				int curH = rect.getHeight();
				if(getCurScaling() != null){
					int oldW = curW;
					int oldH = curH;
					curW = (int)(oldW * getCurScaling().x);
					curH = (int)(oldH * getCurScaling().y);
					curX = getX() + (int)(rect.getX() * getCurScaling().x);
					curY = getY() + (int)(rect.getY() * getCurScaling().y);
				}
//				paint.setStyle(Style.STROKE);
//				paint.setColor(Color.BLUE);
//				canvas.drawRect(curX, curY, curX + curW,  curY + curH, paint);
			}
		}
	}
	
	/**
	 * 绘制背景
	 * @param canvas
	 * @param paint
	 */
	protected void onDrawBackGround(GC gc){
		if(backGround != null){
			backGround.onDraw(gc);
		}
	}
	
	/**
	 * 移动路径是否播放完毕 
	 * @return
	 */
	private boolean isMovePathEnd(){
		if(movePath == null){
			return true;
		}
		if(movePathIndex >= movePath.length - 1){
			return true;
		}
		return false;
	}
	
	/**
	 * 检测是否播放超时
	 */
	private void updataPlayTime(){
		if(isTimeOut()){
//			Log.d(getClass().getSimpleName(), "alpha: " + getCurAlpha() + " scalling: " + getCurScaling().x);
			if(isMovePathEnd()){
				movePath = null;
				setDuration(0);
				if(isUseAfterAnimation()){
					useAfterAnimationData();
				}else{
					useBeforeAnimationData();
				}
				noticeTimeOut();
			}else{
				updataMovePath();
				playNextPath();
			}
		}
	}
	
	/**
	 * 更新移动路径数据
	 */
	private void updataMovePath(){
		if(movePath != null){
			if(movePathIndex < movePath.length - 1){
				setMove(new PointF(movePath[movePathIndex]), new PointF(movePath[movePathIndex + 1]));
				if(isSeemlessPath){
					movePathIndex++;
				}else{
					movePathIndex += 2;
				}
				updataFramIdx();
				initDuration();
			}
		}
	}
	
	/**
	 * 通知动画播放超时 
	 */
	private void noticeTimeOut(){
		if(listeners != null){
			for(int i = 0; i < listeners.size(); i++){
				listeners.elementAt(i).animationTimeOutListener(this);
			}
		}
	}
	
	@Override
	public boolean onKey(int state, KeyEvent keyEvent) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onMouseEvent(int state, MouseEvent event) {
		// TODO Auto-generated method stub
		if(isPlay()){
			if(state == DrawableInterface.EVENT_STATE_DOWN){
				if(isClicked(event.x, event.y)){
					noticeOnTouchListener();
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 通知动画被触碰
	 */
	protected void noticeOnTouchListener(){
		if(listeners != null){
			for(int i = 0; i < listeners.size(); i++){
				listeners.elementAt(i).animationOnTouch(this);
			}
		}
	}
	
	@Override
	public void onUpdate() {
		// TODO Auto-generated method stub
		
	}

	public int getDuration() {
		return duration;
	}

	/**
	 * 设置动画持续时间 单位:毫秒
	 * @param duration
	 */
	public void setDuration(int duration) {
		setDuration(duration, true);
	}
	
	private void updateDurationIdx(){
		durationIdx++;
	}
	
	private void initDuration(){
		if(this.durations != null && durationIdx < durations.length){
			this.duration = durations[durationIdx];
			if(isApplySpeed){
				preDuration = duration / CanvasComposite.delay;
				isDuration = true;
			}
		}
	}
	
	public void setDuration(int[] durations, boolean isApplySpeed) {
		this.durations = durations;
		this.isApplySpeed = isApplySpeed;
		durationIdx = 0;
//		if(this.durations != null && this.durations.length > 0){
//			this.duration = durations[durationIdx];
//		}
//		if(isApplySpeed){
//			preDuration = duration / GameSurface.refreshTime;
//			isDuration = true;
//		}
		initDuration();
		if(duration != 0){
			setLoopNum(-1);
		}
	}
	
	public void setDuration(int duration, boolean isApplySpeed) {
		setDuration(new int[]{duration}, isApplySpeed);
	}
	
	/**
	 * 是否可以播放下一帧
	 * @return
	 */
	public boolean isCanNextFrame(){
		return System.currentTimeMillis() - lastMills >= lastDelay;
	}
	
	/**
	 * 得到当前需要移动的坐标
	 * @return
	 */
	protected PointF getMovePoint(){
		PointF speed = null;
		if(moveDirection != MOVE_DIRECTION_NONE){
			if(isDuration){
				if(preDuration != 0){
					speed = new PointF();
					speed.x = (endPosition.x - startPosition.x) / preDuration;
					speed.y = (endPosition.y - startPosition.y) / preDuration;
				}
			}else{
				if(movePath == null){
					speed = pathSpeed[0];
				}else{
//					if(isSeemlessPath){
						speed = pathSpeed[pathSpeedIndex];
						pathSpeedIndex++;
						if(pathSpeedIndex > pathSpeed.length - 1){
							pathSpeedIndex = 0;
						}
//					}
				}
			}
		}
		
		return speed;
	}
	
	/**
	 * 得到每次旋转角度
	 * @return
	 */
	protected float getPreRotate(){
		if(startRotate != endRotate){
			if(preDuration != 0){
				float pre = (float)(endRotate - startRotate) / preDuration;
				return pre;
			}
		}
		return 0;
	}
	
	/**
	 * 增加转速与旋转角度
	 */
	public void addRotateSpeed(float addPreRotate, int addRotate){
		preRotate += addPreRotate;
		endRotate += addRotate;
	}
	
	public float getPreRotateValue(){
		return preRotate;
	}
	
	public boolean isRoateEnd(){
		return curRotate == endRotate;
	}
	
	public float getCurRoate(){
		return curRotate;
	}
	
	protected void updateRotate(){
		if(preRotate != 0){
			if(curRotate != endRotate){
				if((rotateStyle & ROTATE_STYLE_END_SLOW_DOWN) != 0){
					if(preRotate > 1){
						if(Math.abs(curRotate - endRotate) < Math.abs(preRotate) * 10){
							preRotate *= 0.8;
						}
						if(preRotate < 1){
							preRotate = 1;
						}
					}
				}
				if(preRotate < 0){
					if(curRotate + preRotate < endRotate){
						curRotate = endRotate;
					}else{
						curRotate += preRotate;
					}
				}else{
					if(curRotate + preRotate > endRotate){
						curRotate = endRotate;
					}else{
						curRotate += preRotate;
					}
				}
			}
		}
	}
	
	//不旋转
	public static final byte ROTATE_STYLE_NONE = 0x0;
	//开始旋转时加速
	public static final byte ROTATE_STYLE_START_SPEED_UP = 0x1;
	//结束时减速
	public static final byte ROTATE_STYLE_END_SLOW_DOWN = 0x2;
	//逆时针旋转
	public static final byte ROTATE_STYLE_CCW = 0x4;
	
	private int rotateStyle;
	private float preRotate;
	private int startRotate;
	private int endRotate;
	//当前循环
	private float curCircle;
	//旋转循环次数
	private int rotateCirlcl;
	/**
	 * 设置旋转动画相关参数
	 * @param startRoate
	 * 		起始角度
	 * @param endRotate
	 * 		结束角度
	 * @param style
	 * 		旋转风格
	 */
	public void setRotate(int startRotate, int endRotate, int style){
		this.startRotate = startRotate;
		this.endRotate = endRotate;
		this.rotateStyle = style;
		this.curRotate = this.startRotate;
	}
	
	/**
	 * 设置移动路径
	 * @param points
	 */
	public void setMove(Point[] points, boolean isSeemless){
		movePathIndex = 0;
		this.movePath = points;
		this.isSeemlessPath = isSeemless;
	}
	
	/**
	 * 设置动画移动的起始结束的坐标
	 * @param startPoint
	 * @param endPoint
	 */
	public void setMove(Point startPoint, Point endPoint){
		setMove(new PointF(startPoint), new PointF(endPoint));
	}
	
	/**
	 * 设置动画移动的起始结束的坐标
	 * @param startPoint
	 * @param endPoint
	 */
	public void setMove(PointF startPoint, PointF endPoint){
		this.startPosition = startPoint;
		if(this.startPosition == null){
			if(curPosition != null){
				this.startPosition = new PointF(curPosition.x, curPosition.y);
			}else{
				this.startPosition = new PointF(1, 1);
			}
		}
		if(this.startPosition != null){
			setPositionByMove((int)this.startPosition.x, (int)this.startPosition.y);
		}
		this.endPosition = endPoint;
		curPosition = new PointF(this.startPosition.x, this.startPosition.y);
		moveDirection = getMoveDirection(this.startPosition, this.endPosition);
//		if(this.startPosition == null || endPosition == null
//				|| this.startPosition.equals(endPoint.x, endPoint.y)){
//			moveDirection = MOVE_DIRECTION_NONE;
//		}else{
//			if(this.startPosition.x > endPosition.x){
//				moveDirection |= MOVE_DIRECTION_LEFT;
//			}else{
//				moveDirection |= MOVE_DIRECTION_RIGHT;
//			}
//			if(this.startPosition.y > endPosition.y){
//				moveDirection |= MOVE_DIRECTION_UP;
//			}else{
//				moveDirection |= MOVE_DIRECTION_DOWN;
//			}
//		}
	}
	
	/**
	 * 根据起始点判断移动的方向
	 * @param start
	 * @param end
	 * @return
	 */
	public static byte getMoveDirection(Point start, Point end){
		return getMoveDirection(new PointF(start), new PointF(end));
	}
	/**
	 * 根据起始点判断移动的方向
	 * @param start
	 * @param end
	 * @return
	 */
	public static byte getMoveDirection(PointF start, PointF end){
		byte dir = MOVE_DIRECTION_NONE;
		if(start == null || end == null
				|| start.equals(end.x, end.y)){
			dir = MOVE_DIRECTION_NONE;
		}else{
			if(start.x > end.x){
				dir |= MOVE_DIRECTION_LEFT;
			}else if(start.x < end.x){
				dir |= MOVE_DIRECTION_RIGHT;
			}
			if(start.y > end.y){
				dir |= MOVE_DIRECTION_UP;
			}else if(start.y < end.y){
				dir |= MOVE_DIRECTION_DOWN;
			}
		}
		return dir;
	}
	
	public PointF getCurPoint() {
		return curPosition;
	}

	public PointF getCurScaling() {
		if(curScaling == null){
			return new PointF(1, 1);
		}
		return curScaling;
	}

	public float getCurAlpha() {
		return curAlpha;
	}

	@Override
	public void setPosition(int x, int y) {
		// TODO Auto-generated method stub
		PointF pointF = new PointF(x, y);
		setMove(pointF, pointF);
		super.setPosition(x, y);
	}

	/**
	 * 直接设置动画坐标，不应用于动画移动
	 * @param x
	 * @param y
	 */
	protected void setPositionByMove(int x, int y){
		super.setPosition(x, y);
	}
	
	/**
	 * 设置当前动画的大小
	 * @param scalling
	 */
	public void setCurScaling(PointF scalling){
		setScaling(new PointF(scalling.x, scalling.y), new PointF(scalling.x, scalling.y));
	}
	
	public void setCurAlpha(int alpha){
		setAlpha(alpha, alpha);
	}
	
	public void setScaling(PointF startScaling, PointF endScaling){
		setScaling(startScaling, endScaling, 0);
	}
	
	/**
	 * 设置缩放的起始结束大小
	 * @param startScaling
	 * @param endScaling
	 */
	public void setScaling(PointF startScaling, PointF endScaling, int scallingAnchor){
		this.scallingAnchor = scallingAnchor;
		this.startScaling = startScaling;
		if(this.startScaling == null){
			if(curScaling != null){
				this.startScaling = new PointF(curScaling.x, curScaling.y);
			}else{
				this.startScaling = new PointF(1, 1);
			}
		}
		this.endScaling = endScaling;
		curScaling = new PointF(this.startScaling.x, this.startScaling.y);
		
		scalingType = SCALING_NONE;
		if(this.startScaling == null || endScaling == null
				|| this.startScaling.equals(endScaling.x, endScaling.y)){
			scalingType = SCALING_NONE;
		}else{
			if(this.startScaling.x > endScaling.x){
				scalingType |= SCALING_X_ZOOM_IN;
			}else{
				scalingType |= SCALING_X_ZOOM_OUT;
			}
			if(this.startScaling.y > endScaling.y){
				scalingType |= SCALING_Y_ZOOM_IN;
			}else{
				scalingType |= SCALING_Y_ZOOM_OUT;
			}
		}
	}
	
	/**
	 * 得到每次需要改变的大小
	 * @return
	 */
	protected PointF getPreScaling(){
		if(scalingType != SCALING_NONE && preDuration != 0){
			PointF f = new PointF();
			f.x = (endScaling.x - startScaling.x) / preDuration;
			f.y = (endScaling.y - startScaling.y) / preDuration;
			return f;
		}
		return null;
	}
	
	/**
	 * 设置透明度的起始结束值
	 * @param startAlpha
	 * @param endAlpha
	 */
	public void setAlpha(int startAlpha, int endAlpha){
		this.startAlpha = startAlpha;
		this.endAlpha = endAlpha;
		curAlpha = startAlpha;
	}

	/**
	 * 得到每次需要改变的透明度
	 * @return
	 */
	protected int getPreAlpha(){
		if(startAlpha != endAlpha && preDuration != 0){
			int pre = (endAlpha - startAlpha) / preDuration;
			return pre;
		}
		return -1;
	}
	
	/**
	 * 设置循环次数
	 * @param loopNum
	 */
	public void setLoopNum(int loopNum) {
		this.loopNum = loopNum;
		curLoop = 0;
	}

	/**
	 * 得到动画播放次数
	 * @return
	 */
	public int getLoopNum(){
		return loopNum;
	}
	
	@Override
	public int getHeight() {
		// TODO Auto-generated method stub
		int height = super.getHeight();
		return height;
	}

	public float getRealHeight() {
		// TODO Auto-generated method stub
		float height = super.getHeight();
		if(getCurScaling() != null){
			height = height * getCurScaling().y;
		}
		return height;
	}
	
	@Override
	public int getWidth() {
		// TODO Auto-generated method stub
		int width = super.getWidth();
		return width;
	}

	public float getRealWidth(){
		float width = super.getWidth();
		if(getCurScaling() != null){
			width = width * getCurScaling().x;
		}
		return width;
	}
	
	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		setPlay(false);
	}
	
	
	
	/**
	 * 注册监听器
	 * @param listener
	 */
	public void setListener(IAnimationListener listener){
		if(listeners == null){
			listeners = new Vector<IAnimationListener>();
		}
		listeners.addElement(listener);
	}

	public boolean isUseAfterAnimation() {
		return isUseAfterAnimation;
	}

	private boolean isTimeOut(){
		if(duration != 0){
			boolean isTimeout = System.currentTimeMillis() - startPlayTime > duration;
			boolean isCanNext = isTimeout && isMoveEnd() && isScallingEnd() && isAlphaEnd() && isRoateEnd();
//			if(isTimeout){
//				Log.d(getClass().getSimpleName(), "*********  " + getType() + " move end: " + isMoveEnd());
//			}
//			if(getType() > 32 && isCanNext){
//				Log.d(getClass().getSimpleName(), "*********  " + getType());
//			}
			return isCanNext;
		}
		return false;
	}
	
	public boolean isMoveEnd(){
		if(curPosition != null && endPosition != null){
			return curPosition.equals(endPosition.x, endPosition.y);
		}
		return true;
	}
	
	public boolean isAlphaEnd(){
		if(curAlpha != -1 && endAlpha != 0){
			return curAlpha == endAlpha;
		}
		return true;
	}
	
	public boolean isScallingEnd(){
		if(curScaling != null && endScaling != null){
			return curScaling.equals(endScaling.x, endScaling.y);
		}
		return true;
	}
	
	/**
	 * 设置是否应用动画播放完毕后的数据，如位置、大小、透明度等信息
	 * @param isUseAfterAnimation
	 */
	public void setUseAfterAnimation(boolean isUseAfterAnimation) {
		this.isUseAfterAnimation = isUseAfterAnimation;
	}

	@Override
	public boolean isClicked(int x, int y) {
		// TODO Auto-generated method stub
//		Frame frame = getCurFrame();
//		if( frame != null && frame.getCollideRegions() != null){
//			Vector<XRect> rects = getCurFrame().getCollideRegions();
//			for(int i = 0; i < rects.size(); i++){
//				XRect rect = rects.elementAt(i);
//				if(isContains(rect, x, y)){
//					return true;
//				}
//			}
//			return false;
//		}
		if(rect != null){
			float scalling = 0.2f;
			int curX = rect.getX() + (int)(getWidth() * scalling);
			int curY = rect.getY() + (int)(getHeight() * scalling);
			int curW = (int)(getWidth() * (1 - 2 * scalling));
			int curH = (int)(getHeight() * (1 - 2 * scalling));
			return !(x < curX || x > curX + curW 
					|| y < curY || y > curY + curH);
//			return rect.getRectObject().contains(x, y);
		}
		return false;
	}

	/**
	 * 根据当前帧的缩放 ,检测指定坐标是否在指定碰撞区域中
	 * @param rect
	 * @param x
	 * @param y
	 * @return
	 */
	protected boolean isContains(XRect rect, int x, int y){
		if(rect != null){
			int curX = getX() + rect.getX();
			int curY = getY() + rect.getY();
			int curW = rect.getWidth();
			int curH = rect.getHeight();
			if(getCurScaling() != null){
				int oldW = curW;
				int oldH = curH;
				curW = (int)(oldW * getCurScaling().x);
				curH = (int)(oldH * getCurScaling().y);
				curX = getX() + (int)(rect.getX() * getCurScaling().x);
				curY = getY() + (int)(rect.getY() * getCurScaling().y);
			}
			return !(x < curX || x > curX + curW 
					|| y < curY || y > curY + curH);
		}
		return false;
	}
	
	public byte getSubType() {
		return subType;
	}

	public void setSubType(byte subType) {
		this.subType = subType;
	}

//	public Bitmap getBackGround() {
//		return backGround;
//	}

	public void setBackGround(DrawableInterface backGround) {
		this.backGround = backGround;
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		isPause = true;
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		isPause = false;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		if(frames != null){
			for(int i = 0; i < frames.size(); i++){
				if(frames.elementAt(i) != null){
					frames.elementAt(i).release();
				}
			}
		}
//		super.onDestroy();
		if(backGround != null){
			backGround.onDestroy();
			backGround = null;
		}
//		ResourceManager.getInstance().releaseResource(hashCode(), true);
	}

	/**
	 * 设置每次移动的距离
	 * @param pointF
	 */
	public void setPrePosition(PointF pointF){
		PointF[] speeds = {pointF};
		setPathSpeed(speeds);
//		this.prePosintion = pointF;
//		isDuration = false;
	}
	
	/**
	 * 得到当前播放的循环次数
	 * @return
	 */
	public int getCurLoop(){
		return curLoop;
	}

	/**
	 * 设置各个移动路径的速度
	 * @param pathSpeed
	 */
	public void setPathSpeed(PointF[] pathSpeed) {
		this.pathSpeed = pathSpeed;
		isDuration = false;
	}

	/**
	 * 得到移动移动结束判断条件
	 * @return
	 */
	public int getMoveJudgeStyle() {
		return moveJudgeStyle;
	}

	/**
	 * 设置移动结束判断条件
	 * @param moveStyle
	 */
	public void setMoveJudgeStyle(int moveStyle) {
		this.moveJudgeStyle = moveStyle;
	}
	
//	private boolean isMoveEnd(){
//		if(isMoveJudgeStyleMove2End()){
//			if()
//		}
//	}
	
	/**
	 * 移动结束条件是否是移动至时间结束
	 * @return
	 */
	private boolean isMoveJudgeStyleMoveTimeEnd(){
		return (moveJudgeStyle & MOVE_JUDGE_STYLE_MOVE_TIME_END) != 0;
	}
	
	/**
	 * 移动结束条件是否是移动到终点
	 * @return
	 */
	private boolean isMoveJudgeStyleMove2End(){
		return (moveJudgeStyle & MOVE_JUDGE_STYLE_MOVE_2_END) != 0;
	}
	
	/**
	 * 设置动画风格
	 * @param style
	 */
	public void setStyle(int style){
		this.style = style;
	}
	
	/**
	 * 获取动画播放风格
	 * @return
	 */
	public int getStyle(){
		return style;
	}

	public int getFlip() {
		return flip;
	}

	public void setFlip(int flip) {
		this.flip = flip;
	}

	public boolean isDrawCollideFrame() {
		return isDrawCollideFrame;
	}

	public void setDrawCollideFrame(boolean isDrawCollideFrame) {
		this.isDrawCollideFrame = isDrawCollideFrame;
	}
	
//	public IBitmap getiBackGround() {
//		return iBackGround;
//	}

//	public void setiBackGround(IBitmap iBackGround) {
//		this.iBackGround = iBackGround;
//	}
}
